﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNetExtensions.AspNet
{
    public static class DictionaryExtensions
    {
        public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> keyValuePairs, TKey key, TValue value)
        {
            if (keyValuePairs.ContainsKey(key)) { keyValuePairs[key] = value; }
            else { keyValuePairs.Add(key, value); }
        }

        public static void TryUpdate<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> keyValuePairs, TKey key, TValue value)
        {
            TValue value1 = default(TValue);
            if (keyValuePairs.TryGetValue(key, out value1))
            {
                keyValuePairs.TryUpdate(key, value, value1);
            }
            else { keyValuePairs.TryAdd(key, value); }
        }
    }
}
